package kines.ct;

import kines.SerializableDB;
import java.util.*;
import java.sql.*;
import javax.servlet.http.HttpSession;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Consorcio Terrenos</p>
 *
 * @author Ricardo Ascencio
 * @version 1.0
 */

public class Corrida implements SerializableDB{

  protected int id_corrida;
  protected int id_bloqueo;
  protected int plazo;
  protected double interesMensual;
  protected double gastosAdmon;
  protected double seguro;
  protected double total;
  protected int primerPago;
  protected String enganche;
  protected double engancheTotal;
  protected Calendar fechaFirma;
  protected int tipoPago;
  protected double mensualidad;
  protected double descuento;

  protected Corrida(int id_corrida)throws SQLException, Exception{
    ArrayList rows = new ArrayList();
    String query = "SELECT id_bloqueo,"+
                         " plazo,"+
                         " intmensual,"+
                         " gastosAdmon,"+
                         " seguro,"+
                         " total,"+
                         " primerPago,"+
                         " enganche,"+
                         " engancheTotal,"+
                         " fechaFirma,"+
                         " tipoPago,"+
                         " mensualidad,"+
                         " descuento"+
                    " FROM c_corrida"+
                    " WHERE id_corrida = "+id_corrida;

    Driver DriverRS = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement select = ConnRs.createStatement();

    ResultSet resultado = select.executeQuery(query);

    while(resultado.next()){
      this.id_corrida = id_corrida;
      id_bloqueo = resultado.getInt("id_bloqueo");
      plazo = resultado.getInt("plazo");
      interesMensual = resultado.getDouble("intmensual");
      gastosAdmon = resultado.getDouble("gastosAdmon");
      seguro = resultado.getDouble("seguro");
      total = resultado.getDouble("total");
      primerPago = resultado.getInt("primerpago");
      enganche = resultado.getString("enganche");;
      engancheTotal = resultado.getDouble("engancheTotal");;
      fechaFirma = Calendar.getInstance();
      fechaFirma.setTime(resultado.getDate("fechaFirma"));
      tipoPago = resultado.getInt("tipoPago");
      mensualidad = resultado.getDouble("mensualidad");
      descuento = resultado.getDouble("descuento");
    }
    select.close();
    ConnRs.close();
  }

  /**
     * Not Implement
     * @param session to save message attibute
     * @throws java.lang.Exception
     */
    public void saveData(HttpSession session) throws Exception {
      /**@todo Implement this kines.SerializableDB method*/
      throw new java.lang.UnsupportedOperationException("Method saveData() not yet implemented.");
  }

  /**
    * Not Implement
    * @return
    * @throws java.lang.Exception
    */
   public boolean deleteData() throws Exception {
     /**@todo Implement this kines.SerializableDB method*/
     throw new java.lang.UnsupportedOperationException("Method deleteData() not yet implemented.");
  }

  public static boolean guardaCorrida(int id_bloqueo, int plazo, double interes, double gastosAdmon, double seguro, double total, int primerPago, String enganche, double engancheT, String fechaFirma, int tipoPago, double mensualidad, double descuento)throws SQLException, Exception{
    Driver DriverRS = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement insert = ConnRs.createStatement();

    String query = "EXEC sp_guardarCorrida "+id_bloqueo+","+plazo+","+interes+","+gastosAdmon+","+seguro+","+total+","+primerPago+",'"+enganche+"',"+engancheT+",'"+fechaFirma+"',"+tipoPago+","+mensualidad+","+descuento;
    int si = insert.executeUpdate(query);
    insert.close();
    if(si>0){
      return true;
    }else{
      return false;
    }
  }
}
